package com.example.recruitment_website.service.Impl;

import com.example.recruitment_website.domain.Article;
import com.example.recruitment_website.domain.Collection;
import com.example.recruitment_website.repository.ArticleRepository;
import com.example.recruitment_website.repository.CollectionRepository;
import com.example.recruitment_website.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectionImpl implements CollectionService {
    @Autowired
    CollectionRepository collectionRepository;
    @Autowired
    ArticleRepository articleRepository;
    @Override
    public List<Collection> findAllCollection(int id) {
        return collectionRepository.findByUser_id(id);
    }

    @Override
    public Void addCollection(int id,int userId) {
        collectionRepository.deleteByArticle_idAndUser_id(id,userId);
        Article article = articleRepository.findById(id);
        Collection collection = new Collection();
        collection.setArticle_id(id);
        collection.setUser_id(userId);
        collection.setArticle_title(article.getTitle());
        collection.setDate(article.getDate());
        collection.setImg_url(article.getImg_url());

        collectionRepository.save(collection);

        return null;
    }

    @Override
    public int delCollection(int id,int userId) {
        try {
            collectionRepository.deleteByArticle_idAndUser_id(id,userId);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }
}
